import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Remi Destigny
 * Helper class used to parse JSON strings 
 */
public final class JSONhelper {
	
	public static final String LOCALIZATION_POSE = "Pose";
	public static final String LOCALIZATION_POSITION = "Position";
	public static final String LOCALIZATION_ORIENTATION = "Orientation";
	
	public static final String DIFFERENTIALDRIVE_COMMAND = "Command";
	public static final String DIFFERENTIALDRIVE_FEEDBACK = "Feedback";
	public static final String DIFFERENTIALDRIVE_TARGETANGULARSPEED = "TargetAngularSpeed";
	public static final String DIFFERENTIALDRIVE_TARGETLINEARSPEED = "TargetLinearSpeed";
	public static final String DIFFERENTIALDRIVE_CURRENTANGULARSPEED = "CurrentAngularSpeed";
	public static final String DIFFERENTIALDRIVE_CURRENTLINEARSPEED = "CurrentLinearSpeed";
	
	public static final String LASER_ECHOES = "Echoes";
	
	public static final String PROPERTIES_ANGLEINCREMENT = "AngleIncrement";
	public static final String PROPERTIES_STARTANGLE = "StartAngle";
	public static final String PROPERTIES_ENDANGLE = "EndAngle";
	
	public static ObjectMapper mapper = new ObjectMapper();
	
	/**
	 * Decode a JSON string into a Map ( String key and Object data )
	 * @param jsonString The JSON string to decode
	 * @return A Map with the decode JSON string
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static Map<String, Object> decodeJson(String jsonString) 
			throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(jsonString, Map.class);
	}
	
	/**
	 * Decode a JSON string representing an array
	 * @param jsonString The JSON array string to decode
	 * @return An array containing the decoded JSON string
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static ArrayList<Object> decodeJsonArray(String jsonString) 
			throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(jsonString, new TypeReference<ArrayList<Object>>() {});
	}
}
